from typing import List, Dict
import json


class Interaction:
    def __init__(self, item_id, user_id, type_id, interaction_type=0, info=None):
        self.item_id = item_id
        self.user_id = user_id
        self.type_id = type_id
        self.interaction_type = interaction_type  # 0: Reader
        self.info = info


class FileLoader:
    def __init__(self, hyper_params):
        self.datasets = hyper_params['datasets']
        self.intereaction_list: Dict[int, List[Interaction]] = {}
        self.book_cnt = 0
        self.reader_cnt = 0
        self.type_cnt = 0

        self.type_dict = {}

        self.read_from_dataset_file()
        hyper_params['item_cnt'] = self.book_cnt
        hyper_params['user_cnt'] = self.reader_cnt

    def read_from_dataset_file(self):
        interaction_dict = json.load(open(f'{self.datasets}/interaction_data/interaction_train.json', 'rt'))
        book_list = json.load(open(f'{self.datasets}/book_data/book_list.json', 'rt'))
        user_list = json.load(open(f'{self.datasets}/user_data/user_list.json', 'rt'))

        for user_id, interact_seq in interaction_dict.items():
            user_id = int(user_id)
            self.intereaction_list[user_id] = []
            for interact in interact_seq:
                book_id = interact['book_id']
                cur_type = book_list[str(book_id)]['type']

                if cur_type not in self.type_dict:
                    self.type_dict[cur_type] = len(self.type_dict)

                self.intereaction_list[user_id].append(
                    Interaction(book_id, user_id, self.type_dict[cur_type]))

        self.reader_cnt = len(user_list)
        self.book_cnt = len(book_list)
        self.type_cnt = len(self.type_dict)

        print(
            f'BPRMF: Reader: {self.reader_cnt}, Book: {self.book_cnt}, Interact: {len(self.intereaction_list)}, Type: {self.type_cnt}')


if __name__ == '__main__':
    hyper_params = {
    }

    file_loader = FileLoader(hyper_params=hyper_params)
